package com.atguigu.crowd.handler;

import com.atguigu.crowd.api.MySQLRemoteService;
import com.atguigu.crowd.entity.vo.AddressVO;
import com.atguigu.crowd.entity.vo.MemberLoginVO;
import com.atguigu.crowd.entity.vo.OrderProjectVO;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author yzt
 * @create 2021-09-25 23:45
 */
@Controller
public class OrderHandler {

    @Autowired
    private MySQLRemoteService mySQLRemoteService;


    @RequestMapping("/get/return/info/{projectId}/{returnId}")
    public String showReturnConfirmInfo(
            @PathVariable("projectId") Integer projectId,
            @PathVariable("returnId") Integer returnId, HttpSession session){

        ResultEntity<OrderProjectVO> orderProjectVOResultEntity = mySQLRemoteService.getOrderProjectVORemote(projectId, returnId);

        if("SUCCESS".equals(orderProjectVOResultEntity.getOperationResult())){
            OrderProjectVO orderProjectVO = orderProjectVOResultEntity.getQueryData();
            session.setAttribute("orderProjectVO",orderProjectVO);
        }
        return "confirm-return";
    }

    @RequestMapping("/confirm/order/{returnCount}")
    public String showConfirmOrderInfo(@PathVariable("returnCount") Integer returnCount,
                                        HttpSession session){

        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        orderProjectVO.setReturnCount(returnCount);
        session.setAttribute("orderProjectVO",orderProjectVO);

        MemberLoginVO loginMember = (MemberLoginVO) session.getAttribute("loginMember");
        Integer memberId = loginMember.getId();

        ResultEntity<List<AddressVO>>  resultEntity =mySQLRemoteService.getAddressVORemote(memberId);

        if("SUCCESS".equals(resultEntity.getOperationResult())){
            List<AddressVO> addressVOList = resultEntity.getQueryData();

            session.setAttribute("addressVOList",addressVOList);
        }
        return "confirm-order";
    }

    @RequestMapping("/save/address")
    public String saveAddress(AddressVO addressVO,HttpSession session){

        ResultEntity<String> resultEntity = mySQLRemoteService.saveAddressRemote(addressVO);

        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");

        Integer returnCount = orderProjectVO.getReturnCount();

        return "redirect:http://localhost/order/confirm/order/"+returnCount;
    }
}
